var spring_url='';
var eachPageRows = new Array(10,15,25,50,100);
var timeout_i = 15;   
var isOut = true; 
var methods;
function isfresh(){
    return false;
}
$(document).ready(function(){
	//获取spring路径
	spring_url = window.location.href;
	spring_url = spring_url.substring(0,spring_url.indexOf('/s/')+7)+
		spring_url.substring(spring_url.indexOf('/s/')+7).substring(0,spring_url.substring(spring_url.indexOf('/s/')+7).indexOf("/"));
	//动态为select赋值
	$('select[class*="autoload"]').each(function(){
		var ttobj = $(this);
		$.post(basePath+'/s/ajax/getSysCode?zdlb='+ttobj.attr('zdlb'),function(data){
			var html='<option value=""></option>';
			$.each(data,function(i){
				if(ttobj.text().trim()==data[i].zdbm) html += '<option value="'+data[i].zdbm+'" selected="selected">'+data[i].zdmc+'</option>';
				else  html += '<option value="'+data[i].zdbm+'">'+data[i].zdmc+'</option>';
			});
			
			ttobj.html(html);
		});
	});
	
	$('span[class*="autosearch"]').each(function(){
		 
	});
	
	
	var eachPageRows_html = '';
	for(var i=0;i<eachPageRows.length;i++){
		if($('#form-field-select-2').html()==eachPageRows[i]) eachPageRows_html += '<option value="'+eachPageRows[i]+'" selected="selected">'+eachPageRows[i]+'</option>';
		else  eachPageRows_html += '<option value="'+eachPageRows[i]+'">'+eachPageRows[i]+'</option>';
	}
	$('#form-field-select-2').html(eachPageRows_html);
	//
	$('body').find('div').eq(0).before('<div class="alert alert-success" style="display:none;position: absolute; width:100%;text-align:center;" id="information_prom"></div>');
});


//未指定的select加载内容，需手动调用
function loadSelect(objid,type,param1,param2){
	//动态为select赋值
	var ttobj = $('#'+objid);
	$.post(basePath+'/s/ajax/getSelect',{'type':'yz','param1':param1,'param2':param2},function(data){
		var html='<option value=""></option>';
		$.each(data,function(i){
			if(ttobj.text().trim()==data[i].caption) html += '<option value="'+data[i].id+'" selected="selected">'+data[i].caption+'</option>';
			else  html += '<option value="'+data[i].id+'">'+data[i].caption+'</option>';
		});
		
		ttobj.html(html);
	});
}
/**
 * 加载年度，需手动调用
 * @param {Object} objid 页面显示select对象的id
 * @param {Object} tablename 查询表的名称 
 * @param {Object} zdmc 字段名称
 */
function loadSelect(objid,tablename,zdmc){
	//动态为select赋值
	var ttobj = $('#'+objid);
	$.post(basePath+'/s/ajax/getNd',{'tablename':tablename,'zdmc':zdmc},function(data){
		var html='<option value=""></option>';
		$.each(data,function(i){
			if(ttobj.html()==data[i].caption) html += '<option value="'+data[i].id+'" selected="selected">'+data[i].caption+'</option>';
			else  html += '<option value="'+data[i].id+'">'+data[i].caption+'</option>';
		});
		
		ttobj.html(html);
	});
}
//input页面初始化
function inputload(readonly){
	
	if(readonly.substring(0,1)!='1' && $('#wid').val()!=''){//说明是不可以修改，不显示保存按钮，并将所有input置为disable
		$('button').parent().remove();
		$('.controls').children().attr('disabled','true');
	}
	//表单验证，validate[required,maxSize[50],minSize[2]] 参数说明见/junduMVC/WebRoot/resources/assets/jQuery-Validation-Engine-master/js/languages/jquery.validationEngine-zh_CN.js
	$(".form-horizontal").validationEngine();
	//初始化file
	$('input[type="file"]').each(function(index){
		var ttobj = $(this);
		var html='<form action="'+ basePath+'/s/uploadFile/upload" theme="simple" name="ysform'+index+'" method="post" enctype="multipart/form-data" target="hidden_frame'+index+'">'+
			'<input type="file" id="'+ttobj.attr('id')+'" name="'+ttobj.attr('id')+'" />'+
			'<br/><a onclick="toUpload('+index+')">上传</a><div style="color:red;" id="titlediv"></div>'+
			'</form>'+
			'<iframe name="hidden_frame'+index+'" id="hidden_frame'+index+'" style="display:none"></iframe>';
		ttobj.parent().html(html);
	});
	resizeImg(120,120,'img');
}

var TimeSave = null;
function submit_save(){
	if(validate_save()){
		timeout_i =15;
		isOut = true;
		clearTimeout(TimeSave);
		$('#information_prom').html('');
		parent.YSprogressBlockUI_save();//加载进度条
		var dates = $('input').serialize();
		if($('select').serialize()!=null && $('select').serialize()!='' ) dates += '&'+$('select').serialize();
		
		TimeSave = setTimeout(function(){ 
			 $.ajax({
			     url:spring_url+'/save',
			     type:"POST",
			     data:$("form").serialize(),
				 timeout:1000*3,
				 error:function(XMLHttpRequest, textStatus, errorThrown){
					 $('#information_prom').html("请求超时！");
					 parent.unblockUI();
				 },
				 success:function(data){
					 	if(data.error==null && ( data.wid==null || data.wid=='')){
					 		parent.openLogin('save,'+window.location.href);
					 	}
					    $('#information_prom').css({'filter':'progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=50,finishOpacity=50)','opacity':100,'display':''});
					    if(data.error==null){
							$('#information_prom').html("保存成功！");
							timeout("information_prom");
							$('#wid').val(data.wid);
							
						}else{
							$('#information_prom').html(data.error);
						}
						parent.unblockUI();
					 }
			    });
			clearTimeout(TimeSave); 
		}, 400); 
	}
}

function validate_save(){
	return true;
}
function open_showPrompt(objid,promptText){
	methods._showPrompt($('#'+objid), promptText, "", false,$('form').data('jqv'));
}
/**
 * 查询
 */
function super_doSearch(){
	//设置为第一页
	$('#yspager_currentPageno').val('1');
	document.forms["ysform"].submit();
}
/**
 * 重置
 */
function super_reset(){
	$('#userSelectSearch').find('table input').val('');//重置input内容为空
	//重置select值为第一个option的值
	$('#userSelectSearch').find('table select').each(function(){
		$(this).val($(this).find('option').eq(0).attr('value'));
	});
}
/**
 * 新增或者点击内容超链接
 */
function openEntity(id,parames){
	var url = '';
	if(id==null) url = spring_url+'/openCreate';
	else url = spring_url+'/input/'+id;
	if(id==null){
		var nd=$('#nd').val();
		var jd=$('#jd').val();
		var yf=$('#yf').val();
		var pc=$('#pc').val();
		if(!nd) nd='';
		if(!jd) jd='';
		if(!yf) yf='';
		if(!pc) pc='';
		var action = spring_url.substring(spring_url.lastIndexOf("/")+1);
		//异步提交
	    $.post(basePath+'/s/biz/ajaxShp/sbsp',{'type':'yz','nd':nd,'jd':jd,'yf':yf,'pc':pc,'action':action},function(data){
	    	if(data.jsonstr=='success'){
	    		openview(id,url,parames);
	    	}else{
	    		alert(data.jsonstr);
	    	}
	    });
	}else{
		openview(id,url);
	}
	
}

function openview(id,url,parames){
	if(id==''||id==null){
		var nd=$('#nd').val();
		var jd=$('#jd').val();
		var yf=$('#yf').val();
		var pc=$('#pc').val();
		if(!nd) nd='';
		if(!jd) jd='';
		if(!yf) yf='';
		if(!pc) pc='';
		url += '?nd='+nd+'&jd='+jd+'&yf='+yf+'&pc='+pc;
		if(parames!='' && parames!=null) url += '&'+parames;
	}else{
		url += '/2'+Math.ceil(Math.random()*10000000);
	}
	var name = $('#td'+id).text().trim();
	if((name==null || name=='') && (id=='' || id==null)) name='新增';
	else if(name==null || name==''){
		name = $('input[value="'+id+'"]').parent().next('td').text().trim();
	}
	parent.addTab(name,url,"1",'1');
}
/**
 * 修改
 */
function doModify(){
	var slength = 0;
	var a = document.getElementsByName("selectNode");

	for (var i = 0; i < a.length; i++) {
	    if (a[i].checked) {
	    	slength += 1;
	    	var id = a[i].value;
	    	var url = spring_url+'/input/'+id+'/1'+Math.ceil(Math.random()*10000000);
	    	var name = $('#td'+id).text().trim();
	    	if((name==null || name=='') && (id=='' || id==null)) name='新增';
			else if(name==null || name==''){
				name = $('input[value="'+id+'"]').parent().next('td').text().trim();
			}
	    	
	    	var nd=$('#nd').val();
			var jd=$('#jd').val();
			var yf=$('#yf').val();
			var pc=$('#pc').val();
			if(!nd) nd='';
			if(!jd) jd='';
			if(!yf) yf='';
			if(!pc) pc='';
			var action = spring_url.substring(spring_url.lastIndexOf("/")+1);
			//异步验证是否可以删除
		    $.post(basePath+'/s/biz/ajaxShp/sbsp',{'type':'yz','nd':nd,'jd':jd,'yf':yf,'pc':pc,'action':action},function(data){
		    	if(data.jsonstr=='success'){
		    		parent.addTab(name,url,"1",'1');
		    	}else{
		    		alert(data.jsonstr);
		    	}
		    });
			
	    }
	}
	if(slength==0){
		alert("请勾选一条记录!");
	}
}
/**
 * 删除
 */
function doRemove(){
	var slength = 0;
	var a = document.getElementsByName("selectNode");

	for (var i = 0; i < a.length; i++) {
	    if (a[i].checked) {
	    	slength += 1;
	    	
	    }
	}
	if(slength>0){
		var id = CropCheckBoxValueAsString("selectNode");
		
		var nd=$('#nd').val();
		var jd=$('#jd').val();
		var yf=$('#yf').val();
		var pc=$('#pc').val();
		if(!nd) nd='';
		if(!jd) jd='';
		if(!yf) yf='';
		if(!pc) pc='';
		var action = spring_url.substring(spring_url.lastIndexOf("/")+1);
		//异步验证是否可以删除
	    $.post(basePath+'/s/biz/ajaxShp/sbsp',{'type':'yz','nd':nd,'jd':jd,'yf':yf,'pc':pc,'action':action},function(data){
	    	if(data.jsonstr=='success'){
	    		//异步提交删除
	    	    $.post(spring_url+'/remove',{'wid':id},function(data){
	    	    	var ids = id.split(',');
	    	    	for(var i=0;i<ids.length;i++){
	    	    		if(ids[i]!='') $('#'+ids[i]).parent().parent().remove();
	    	    	}
	    	    });
	    	}else{
	    		alert(data.jsonstr);
	    	}
	    });
		
	}else{
		alert("请勾选要删除的记录!");
	}
}

/**
 * 删除
 */
function doRemoveByid(id){
	var nd=$('#nd').val();
	var jd=$('#jd').val();
	var yf=$('#yf').val();
	var pc=$('#pc').val();
	if(!nd) nd='';
	if(!jd) jd='';
	if(!yf) yf='';
	if(!pc) pc='';
	var action = spring_url.substring(spring_url.lastIndexOf("/")+1);
	//异步验证是否可以删除
    $.post(basePath+'/s/biz/ajaxShp/sbsp',{'type':'yz','nd':nd,'jd':jd,'yf':yf,'pc':pc,'action':action},function(data){
    	if(data.jsonstr=='success'){
    		//异步提交删除
    	    $.post(spring_url+'/remove',{'wid':id},function(data){
    	    	var ids = id.split(',');
    	    	for(var i=0;i<ids.length;i++){
    	    		if(ids[i]!='') $('#'+ids[i]).parent().parent().remove();
    	    	}
    	    });
    	}else{
    		alert(data.jsonstr);
    	}
    });
}

/**
 * 点击上传
 * @param {Object} i
 */
function toUpload(i){
	$.blockUI(YSprogressBlockUI());
	document.forms["ysform"+i].submit();
}
/**
 * 图片等比例缩放(已长和宽最小的一边为准)
 * @param {Object} _boxWidth 缩放后的宽度
 * @param {Object} _boxHeight 缩放后的高度
 * @param {Object} _imgName 要缩放的图片的名字中的关键字
 * @memberOf {TypeName}
 */
function resizeImg(_boxWidth, _boxHeight, _imgName) {
	var boxWidth = 250;
	var boxHeight = 250;
	var imgName = "img";
	if (_boxWidth && _boxWidth > 0) {
		boxWidth = _boxWidth;
	}
	if (_boxHeight && _boxHeight > 0) {
		boxHeight = _boxHeight;
	}
	if (_imgName && _imgName != "") {
		imgName = _imgName;
	}
	
	$("img[name*='" + imgName + "']").each(function(i){
		var imgWidth = $(this).width();
		var imgHeight=$(this).height();
		 if ((boxWidth / boxHeight) >= (imgWidth / imgHeight)) {
		        //重新设置imgwidthheight
			 $(this).attr("width",boxWidth);
			 $(this).attr("height",((boxWidth * imgHeight) / imgWidth));
		    }
		    else {
		    	 //重新设置imgwidthheight
		    	$(this).attr("width",((boxHeight * imgWidth) / imgHeight));
				$(this).attr("height",boxHeight);
		    }
	});
}
/**
 * 将提示关闭
 * @param {Object} objid
 */
function timeout(objid){
	if (timeout_i>=0 && isOut) {timeout_i--;$('#'+objid).css({'filter':'progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=50,finishOpacity=50)','opacity':10*timeout_i*0.1/15});
		setTimeout('timeout(\''+objid+'\')',200);//渐变    
	}    
	else{isOut = !isOut;$('#'+objid).html('');$('#'+objid).css('display','none')}    
	
}

function doUniChangeState(type,obj){
	if(type=='0'){//禁用
		
	}else if(type=='1'){//启用
		
	}else if(type=='2'){//审核
		var slength = 0;
		var a = document.getElementsByName("selectNode");
	
		for (var i = 0; i < a.length; i++) {
		    if (a[i].checked) {
		    	slength += 1;
		    	
		    }
		}
		if(slength==1){
			if(document.getElementById('process_tg')){
				$('#process_tgyj').val('');
				$('#process_tg').click();
			}else{
				doUniChangeState('22');
			}
		}else{
			alert("请勾选一条要审核的数据!");
		}
	}else if(type=='22'){//审核
		var dwbm = CropCheckBoxValueAsString("selectNode");
		var nd=$('#nd').val();
		var jd=$('#jd').val();
		var yf=$('#yf').val();
		var pc=$('#pc').val();
		if(!nd) nd='';
		if(!jd) jd='';
		if(!yf) yf='';
		if(!pc) pc='';
		var process_tgyj = $('#process_tgyj').val();
		var process_tgsj = $('#process_tgsj').val();
		var action = spring_url.substring(spring_url.lastIndexOf("/")+1);
		//异步提交
	    $.post(basePath+'/s/biz/ajaxShp/sbsp',{'dwbm':dwbm,'type':'2','nd':nd,'jd':jd,'yf':yf,'pc':pc,'action':action,'spyj':process_tgyj,'spsj':process_tgsj},function(data){
	    	alert(data.jsonstr);
	    	super_doSearch();
	    });
	}else if(type=='-2'){//退回
		var slength = 0;
		var a = document.getElementsByName("selectNode");
	
		for (var i = 0; i < a.length; i++) {
		    if (a[i].checked) {
		    	slength += 1;
		    	
		    }
		}
		if(slength==1){
			$('#process_thyj').val('');
			$('#process_th').click();
		}else{
			alert("请勾选一条要审核的数据!");
		}
		
	}else if(type=='-3'){//退回
		 
		var dwbm = CropCheckBoxValueAsString("selectNode");
		var nd=$('#nd').val();
		var jd=$('#jd').val();
		var yf=$('#yf').val();
		var pc=$('#pc').val();
		if(!nd) nd='';
		if(!jd) jd='';
		if(!yf) yf='';
		if(!pc) pc='';
		var action = spring_url.substring(spring_url.lastIndexOf("/")+1);
		var process_thyj = $('#process_thyj').val();
		//异步提交
	    $.post(basePath+'/s/biz/ajaxShp/sbsp',{'dwbm':dwbm,'type':'-2','nd':nd,'jd':jd,'yf':yf,'pc':pc,'action':action,'spyj':process_thyj},function(data){
	    	alert(data.jsonstr);
	    	super_doSearch();
	    });
		 
	}else if(type=='3'){//上报
		var dwbm = "";
		var nd=$('#nd').val();
		var jd=$('#jd').val();
		var yf=$('#yf').val();
		var pc=$('#pc').val();
		if(!nd) nd='';
		if(!jd) jd='';
		if(!yf) yf='';
		if(!pc) pc='';
		var action = spring_url.substring(spring_url.lastIndexOf("/")+1);
		//异步提交
	    $.post(basePath+'/s/biz/ajaxShp/sbsp',{'dwbm':dwbm,'type':type,'nd':nd,'jd':jd,'yf':yf,'pc':pc,'action':action},function(data){
	    	alert(data.jsonstr);
	    	super_doSearch();
	    });
	}
}

//查看流程申请进度
function processjd(dwbm){
	var action = spring_url.substring(spring_url.lastIndexOf("/")+1);
	var nd=$('#nd').val();
	var jd=$('#jd').val();
	var yf=$('#yf').val();
	var pc=$('#pc').val();
	if(!nd) nd='';
	if(!jd) jd='';
	if(!yf) yf='';
	if(!pc) pc='';
	
	parent.addTab("查看流程进度",basePath+'/system/process-ckjd.action?dwbm='+dwbm+'&tsysProcess.actionname='+action
			+'&nd='+nd+'&jd='+jd+'&yf='+yf+'&pc='+pc,"1",'1');
}

function doExport(){
	
}